/**
 * 20080205 0935: Eric Kao, 使用常數傳入, 來取得 Logger<br>
 * 20080102 1648: Eric Kao, E885Exception class move to com.e885.exception.
 */

package com.e885.util.date;

import java.text.ParseException;
import java.util.Date;

import com.e885.bundle.E885UtilResourceBundle;
import com.e885.constant.util.DirectionConstant;
import com.e885.exception.E885UtilException;
import com.e885.log.E885Logger;
import com.e885.util.string.StringUtil;

/**
 * 日期轉成字串, 或字串轉成日期的 tools<br>
 * <br>
 * Copyright: Copyright (c) 2001-2008<br>
 * Company: E885 Group
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 */
public class DateStringUtil
{
	/**
	 * 取得目前系統日期時間, 並格式我們要的格式
	 * 
	 * @param formatSytle 格式化的格式
	 * @return 系統日期時間字串
	 */
	public static String fetchCurrentDateString( String pattern )
	{
		return transDate2String( new Date(), pattern );
	}

	/**
	 * pase 2006/01/01, or 2006/1/1, or 20060101 to 20060101
	 * 
	 * @param source 來源的日期字串
	 * @return String 整合後的字串
	 */
	public static String integrateCenturyDateString( String source )
	{
		if( source == null )
			throw new IllegalArgumentException( "source is null" );

		String[] strs = source.split( "/" );

		int length = strs.length;

		if( length == 1 )
			return source;
		else
		{
			if( length != 3 )
				throw new IllegalStateException( "source fomrat is error" );

			return strs[ 0 ] + StringUtil.pendingZero( strs[ 1 ], 2, DirectionConstant.FRONT )
				+ StringUtil.pendingZero( strs[ 2 ], 2, DirectionConstant.FRONT );
		}
	}

	/**
	 * integrate 970302 or 0970302, or 97/03/02, or 97/3/2 to 0970302
	 * 
	 * @param source
	 * @return
	 */
	public static String integrateTWDateString( String source )
	{
		if( source == null )
			throw new IllegalArgumentException( "source is null" );

		String[] strs = source.split( "/" );

		int length = strs.length;

		if( length == 1 )
			return StringUtil.pendingZero( source, 7, DirectionConstant.FRONT );
		else
		{
			if( length != 3 )
			{
				throw new IllegalStateException( "source fomrat is error" );
			}

			StringBuffer buffer = new StringBuffer();

			buffer.append( StringUtil.pendingZero( strs[ 0 ], 3, DirectionConstant.FRONT ) );
			buffer.append( StringUtil.pendingZero( strs[ 1 ], 2, DirectionConstant.FRONT ) );
			buffer.append( StringUtil.pendingZero( strs[ 2 ], 2, DirectionConstant.FRONT ) );

			return buffer.toString();
		}
	}

	/**
	 * 將 Date 物件 format 成指定 <code>E885DateFormat</code> 的字串.<br>
	 * 可自動轉成台灣日期的字串
	 * 
	 * @param date
	 * @param formatStyle
	 * @return
	 */
	public static String transDate2String( Date date, E885DateFormat format )
	{
		if( date == null )
			throw new IllegalArgumentException( "date is null" );

		if( format == null )
			throw new IllegalArgumentException( "format is null" );

		return format.formatDate( date );
	}

	/**
	 * see transDate2String( date:Date, format:E885DateFormat )
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String transDate2String( Date date, String pattern )
	{
		return transDate2String( date, new E885DateFormat( pattern ) );
	}

	/**
	 * see transString2Date(value:String, format:E885DateFormat)
	 * 
	 * @param value
	 * @param pattern
	 * @return
	 * @throws E885UtilException
	 */
	public static Date transString2Date( String value, String pattern ) throws E885UtilException
	{
		return transString2Date( value, new E885DateFormat( pattern ) );
	}

	/**
	 * 將字串 format 成指定 <code>E885DateFormat</code> 的 Date 物件.<br>
	 * 目前只有轉換西元日期<br>
	 * 
	 * @param value
	 * @param formatStyle
	 * @return
	 * @throws E885UtilException
	 */
	public static Date transString2Date( String value, E885DateFormat format ) throws E885UtilException
	{
		if( value == null || value.trim().length() == 0 )
			return null;

		try
		{
			return format.parse( value );
		}
		catch( ParseException ex )
		{
			String message = E885UtilResourceBundle.getString( "e885Util.dateStringUtil.error.format" );

			message = message + "[" + value + "]";

			E885Logger.utilLog.error( message );

			throw new E885UtilException( message, ex );
		}
	}
}
